package com.tompai.web.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.tompai.elfinder.ElFinderConstants;
import com.tompai.elfinder.command.CommandFactory;
import com.tompai.elfinder.core.Volume;
import com.tompai.elfinder.core.VolumeSecurity;
import com.tompai.elfinder.core.impl.DefaultVolumeSecurity;
import com.tompai.elfinder.core.impl.SecurityConstraint;
import com.tompai.elfinder.param.Node;
import com.tompai.elfinder.service.ElfinderStorage;
import com.tompai.elfinder.service.ElfinderStorageFactory;
import com.tompai.elfinder.service.VolumeSources;
import com.tompai.elfinder.service.impl.DefaultElfinderStorage;
import com.tompai.elfinder.service.impl.DefaultElfinderStorageFactory;
import com.tompai.elfinder.service.impl.DefaultThumbnailWidth;
import com.tompai.elfinder.support.locale.LocaleUtils;

@Configuration
public class ElFinderConfig {

	@Autowired
	private ElfinderConfiguration elfinderConfiguration;

	@Bean(name = "commandFactory")
	public CommandFactory getCommandFactory() {
		CommandFactory commandFactory = new CommandFactory();
		commandFactory.setClassNamePattern("com.tompai.elfinder.command.%sCommand");
		return commandFactory;
	}

	@Bean(name = "elfinderStorageFactory")
	public ElfinderStorageFactory getElfinderStorageFactory() {
		DefaultElfinderStorageFactory elfinderStorageFactory = new DefaultElfinderStorageFactory();
		elfinderStorageFactory.setElfinderStorage(getElfinderStorage());
		return elfinderStorageFactory;
	}

	@Bean(name = "elfinderStorage")
	public ElfinderStorage getElfinderStorage() {

		DefaultElfinderStorage defaultElfinderStorage = new DefaultElfinderStorage();

		// creates thumbnail 缩略图
		DefaultThumbnailWidth defaultThumbnailWidth = new DefaultThumbnailWidth();
		defaultThumbnailWidth.setThumbnailWidth(elfinderConfiguration.getThumbnail().getWidth().intValue());

		// creates volumes, volumeIds, volumeLocale and volumeSecurities
		Character defaultVolumeId = 'A';
		List<Node> elfinderConfigurationVolumes = elfinderConfiguration.getVolumes();
		List<Volume> elfinderVolumes = new ArrayList<>(elfinderConfigurationVolumes.size());
		Map<Volume, String> elfinderVolumeIds = new HashMap<>(elfinderConfigurationVolumes.size());
		Map<Volume, Locale> elfinderVolumeLocales = new HashMap<>(elfinderConfigurationVolumes.size());
		List<VolumeSecurity> elfinderVolumeSecurities = new ArrayList<>();

		// creates volumes
		for (Node elfinderConfigurationVolume : elfinderConfigurationVolumes) {

			final String alias = elfinderConfigurationVolume.getAlias();
			final String path = elfinderConfigurationVolume.getPath();
			final String appkey = elfinderConfigurationVolume.getAppkey();
			final String source = elfinderConfigurationVolume.getSource();
			final String locale = elfinderConfigurationVolume.getLocale();
			final boolean isLocked = elfinderConfigurationVolume.getConstraint().isLocked();
			final boolean isReadable = elfinderConfigurationVolume.getConstraint().isReadable();
			final boolean isWritable = elfinderConfigurationVolume.getConstraint().isWritable();

			// creates new volume
//            Volume volume = VolumeSources.of(source).newInstance(alias, path);
			Volume volume = VolumeSources.of(source).newInstance(alias, path, appkey);

			elfinderVolumes.add(volume);
			elfinderVolumeIds.put(volume, Character.toString(defaultVolumeId));
			elfinderVolumeLocales.put(volume, LocaleUtils.toLocale(locale));

			// creates security constraint
			SecurityConstraint securityConstraint = new SecurityConstraint();
			securityConstraint.setLocked(isLocked);
			securityConstraint.setReadable(isReadable);
			securityConstraint.setWritable(isWritable);

			// creates volume pattern and volume security
			final String volumePattern = Character.toString(defaultVolumeId)
					+ ElFinderConstants.ELFINDER_VOLUME_SERCURITY_REGEX;
			elfinderVolumeSecurities.add(new DefaultVolumeSecurity(volumePattern, securityConstraint));

			// prepare next volumeId character
			defaultVolumeId++;
		}

		defaultElfinderStorage.setThumbnailWidth(defaultThumbnailWidth);
		defaultElfinderStorage.setVolumes(elfinderVolumes);
		defaultElfinderStorage.setVolumeIds(elfinderVolumeIds);
		defaultElfinderStorage.setVolumeLocales(elfinderVolumeLocales);
		defaultElfinderStorage.setVolumeSecurities(elfinderVolumeSecurities);

		return defaultElfinderStorage;
	}

}
